package zhongbo.java.air.flightpath.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.flightpath.listener.RedisLogChaneIPublisher;
import zhongbo.java.air.flightpath.mapper.FlightPathAddMapper;
import zhongbo.java.air.flightpath.mapper.FlightPathMapper;
import zhongbo.java.air.flightpath.model.FlightPath;
import zhongbo.java.air.flightpath.model.FlightPathl;
import zhongbo.java.air.flightpath.service.IFlightPathService;

import java.util.Date;
import java.util.List;

@Service
public class FlightPathImpl implements IFlightPathService {
    @Autowired
    private FlightPathMapper flightPathMapper;
    @Autowired
    private FlightPathAddMapper flightPathAddMapper;
    @Autowired
    private RedisLogChaneIPublisher publisher;
    @Override
    public FlightPath add(FlightPath flightPath) {
    ValidFlightPath(flightPath);
    flightPath.setTimestamp(new Date());
    int addPath=flightPathMapper.insert(flightPath);
    if (addPath>0){
        // 发布日志
        publisher.sendMessage(
                String.format("创建航班轨迹 %s (%s->%s)",
                        flightPath.getSourceType(),
                        flightPath.getIcaoCode(),
                        flightPath.getTimestamp())
        );
        return flightPath;
    }
        throw new RuntimeException("添加错误");
    }
private void ValidFlightPath(FlightPath flightPath){
        String validCode=flightPath.getIcaoCode();
        if (StrUtil.isBlank(validCode)){
            throw new RuntimeException("关联飞机ICAO码不能为空");
        }
    List<FlightPath>listValid=flightPathMapper.selectList(null);
    for (FlightPath path : listValid) {
        if (path.getIcaoCode().equals(flightPath.getIcaoCode())){
            throw new RuntimeException("关联飞机ICAO码不能重复");
        }
    }
}
    @Override
    public String delete(String id) {
        if (StrUtil.isBlank(id)){
            throw new RuntimeException("输入id进行删除");
        }
        int delete=flightPathMapper.deleteById(id);
        if (delete>0){
            publisher.sendMessage(
                    String.format("删除航迹"+id)
            );
            return id;
        }
        throw new RuntimeException("删除错误");
    }

    @Override
    public FlightPath edit(FlightPath edit) {
        String editId=edit.getId();
        ValidFlightPath(edit);
        if (StrUtil.isBlank(editId)){
            throw new RuntimeException("输入id进行修改");
        }
        edit.setTimestamp(new Date());
      int update=flightPathMapper.updateById(edit);
        if (update>0){
            publisher.sendMessage(
                    String.format("修改航班轨迹 %s (%s->%s)",
                    edit.getSourceType(),
                    edit.getIcaoCode(),
                    edit.getTimestamp())
            );
            return edit;
        }
        throw new RuntimeException("修改错误");
    }

    @Override
    public FlightPath findFlightPath(String icaoCode) {
        if (StrUtil.isBlank(icaoCode)){
            throw new RuntimeException("输入icaoCode地址码进行查询");
        }
        publisher.sendMessage(
                String.format("查询航班轨迹"+icaoCode)
        );
        return flightPathMapper.selectById(icaoCode);
    }

    @Override
    public PageVo<FlightPathl> pageVo(Integer page, Integer pageSize
            , String icao, String startTime, String endTime
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<FlightPathl> queryWrapper = new QueryWrapper<>();if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){

            if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
                String[] fieldsArray = fields.split(",");
                for (String field : fieldsArray) {
                    queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
                }
            }
        }


        if (StrUtil.isNotBlank(icao)) {
            // 精准查询cao
            queryWrapper.eq("icao_code", icao);
        }
        //大于等于
        if (startTime !=null) {
            queryWrapper.ge("timestamp", startTime);
        }
        //小于等于
        if (endTime !=null) {
            queryWrapper.le("timestamp", endTime);
        }

        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(oderByField);
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(oderByField);
            }
        }
        Page<FlightPathl> pageParam = new Page<>(page, pageSize);
        Page<FlightPathl> userPage = flightPathAddMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(userPage);

    }
}
